﻿using System;
using System.Xml.Serialization;
using System.IO;

[XmlInclude(typeof(AssetBooks))]
[XmlInclude(typeof(AssetUnity))]
public abstract class Asset {
	// === Public =====================================================================================================
	public string PathRelativeToLib { get; set; }
	public long SizeInBytes { get; set; }
	public string Name { get; set; }
	public string Extension { get; set; }
	[XmlIgnore]
	public FileInfo FileInfo { get; private set; }

	// for xml deserialization
	public Asset() { }

	public void RefreshAsset(string libFullName) {
		FileInfo = new FileInfo(Path.Combine(libFullName, PathRelativeToLib, Name + "." + Extension));
	}

	// === Protected ==================================================================================================
	protected abstract string GetAssetName(FileInfo fileInfo);

	protected Asset(LibraryBase library, FileInfo fileInfo) {
		if(fileInfo.FullName.StartsWith(library.LibPath)) {
			PathRelativeToLib = fileInfo.FullName.Substring(library.LibPath.Length + 1, fileInfo.FullName.Length - library.LibPath.Length - fileInfo.Name.Length - 1);
		} else {
			throw new Exception("incorrect start");
		}
		SizeInBytes = fileInfo.Length;
		Name = GetAssetName(fileInfo);
		Extension = fileInfo.Extension.Substring(1);
		RefreshAsset(library.LibPath);
	}
}